package com.youease.chris
{
	import com.youease.chris.control.MenuBarCommand;
	import com.youease.chris.control.StartUpCommand;
	import com.youease.chris.manager.GlobalObject;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.IFacade;
	import org.puremvc.as3.patterns.facade.Facade;
	
	public class MyAppFacade extends Facade implements IFacade
	{
		public static const APP_STARTUP:String = "app_startup";
		public static const APP_STARTUP_OVER:String = "app_startup_over";
		private var loader:URLLoader;
		
		public function MyAppFacade()
		{
			super();
		}
		
		public static function getInstance():MyAppFacade{
			if(instance==null) instance = new MyAppFacade();
			return instance as MyAppFacade;
		}									
		override protected function initializeController():void{
			super.initializeController();
			//register some Commands
			registerCommand(APP_STARTUP,StartUpCommand);
		}
		
		override protected function initializeView():void {
			super.initializeView();
		}
		public function startup(app:Object):void{		
			sendNotification(APP_STARTUP,app);	
			readConfig();
		}
		
		private function readConfig():void {
			loader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE,onConfigLoaded);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			
			var urlRequest:URLRequest = new URLRequest("config.xml");
			loader.load(urlRequest);
		}
		
		private function onConfigLoaded(evt:Event):void {
			loader.removeEventListener(Event.COMPLETE,onConfigLoaded);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			
			var config:XML = new XML(loader.data);
			GlobalObject.config = config;
			for each(var type:XML in config.child("textureType").child("type")) {
				GlobalObject.texturesType[type.@name] = type.@id;
				GlobalObject.texturesName[type.@id] = type.@desc+"("+type.@name+")";
				GlobalObject.texturesEnName[type.@id] = type.@name;
			}
		}
		
		private function onError(evt:Event):void {
			trace("读取配置文件出错");
		}
	}
}